//
//  XHTrafficBuyBackViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/17.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHTrafficBuyBackViewController.h"
#import "XHBuyBackBounceViewController.h"

#import "XHTrafficStateCell.h"
#import "XHTrafficContentCell.h"
#import "XHBuyBackCell.h"

#import "Services.h"

@interface XHTrafficBuyBackViewController ()<XHBuyBackCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSArray * useInfoArray;
@end

@implementation XHTrafficBuyBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHTrafficStateCell" bundle:nil] forCellReuseIdentifier:XHTrafficStateCellIdentifier];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHTrafficContentCell" bundle:nil] forCellReuseIdentifier:XHTrafficContentCellIdentifier];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHBuyBackCell" bundle:nil] forCellReuseIdentifier:XHBuyBackCellIdentifier];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self orderSnUseFinancialBenefitsDetail];
}

- (void)orderSnUseFinancialBenefitsDetail
{
    [ServiceManager OrderSnUseFinancialBenefitsWithOrderSn:self.backInfo[@"orderSn"] success:^(NSDictionary *data) {
        self.useInfoArray = (NSArray*)data;
        [self.ui_tableView reloadData];
    } failure:^(NSDictionary *data) {
        
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger section = 0;
    if ([self.useInfoArray isKindOfClass:[NSArray class]])
    {
        if (self.useInfoArray.count!=0)
        {
            section = 3;
        }
        else
        {
            section = 2;
        }
    }
    else
    {
        section=2;
    }
    return section;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows=0;
    if ([self.useInfoArray isKindOfClass:[NSArray class]])
    {
        if (self.useInfoArray.count!=0)
        {
            if (section==0)
            {
                rows=1;
            }
            else if (section==1)
            {
                rows=self.useInfoArray.count;
            }
            else if(section==2)
            {
                rows=1;
            }
        }
        else
        {
            if (section==0)
            {
                rows=1;
            }
            else if (section==1)
            {
                rows=1;
            }
        }
    }
    else
    {
        if (section==0)
        {
            rows=1;
        }
        else if (section==1)
        {
            rows=1;
        }
    }
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if ([self.useInfoArray isKindOfClass:[NSArray class]])
    {
        if (self.useInfoArray.count!=0)
        {
            if (indexPath.section==0)
            {
                cell = [self trafficContentCell:indexPath];
            }
            else if (indexPath.section==1)
            {
                cell = [self trafficStateCell:indexPath];
            }
            else if (indexPath.section==2)
            {
                cell = [self buyBackCell];
            }
        }
        else
        {
            if (indexPath.section==0)
            {
                cell = [self trafficContentCell:indexPath];
            }
            else if (indexPath.section==1)
            {
                cell = [self buyBackCell];
            }
        }
    }
    else
    {
        if (indexPath.section==0)
        {
            cell = [self trafficContentCell:indexPath];
        }
        else if (indexPath.section==1)
        {
            cell = [self buyBackCell];
        }
    }
    return cell;
}

- (XHTrafficContentCell*)trafficContentCell:(NSIndexPath*)indexPath
{
    XHTrafficContentCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHTrafficContentCellIdentifier];
    NSDictionary * fundDic = self.backInfo[@"fundProfile"];
    NSArray * operatorArray = fundDic[@"products"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.ui_time.text = [[NSDate dateWithString:self.backInfo[@"recDate"]] stringWithFormatter];
    cell.ui_goodCount.text = [NSString stringWithFormat:@"%@/%@",fundDic[@"totalRemainCount"],fundDic[@"totalOriginalCount"]];
    cell.ui_money.text = [NSString stringWithFormat:@"￥%.2f",[self.backInfo[@"principal"] doubleValue]];
    NSMutableArray * array = [[NSMutableArray alloc]init];
    for (NSString * str in operatorArray)
    {
        if([str isEqualToString:@"10086"])
        {
            [array addObject:@"移动流量"];
        }
        else if ([str isEqualToString:@"10010"])
        {
            [array addObject:@"联通流量"];
        }
        else if ([str isEqualToString:@"10000"])
        {
            [array addObject:@"电信流量"];
        }
    }
    NSString * operator = [array componentsJoinedByString:@" "];
    cell.ui_goodType.text = [NSString stringWithFormat:@"%@",operator];
    return cell;
}

- (XHTrafficStateCell*)trafficStateCell:(NSIndexPath*)indexPath
{
    XHTrafficStateCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHTrafficStateCellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary * dic = self.useInfoArray[indexPath.row];
    cell.ui_time.text = [[NSDate dateWithString:dic[@"operateTime"] ]stringDateYear];
    NSString * unit = @"";
    NSInteger values = [dic[@"productValue"] integerValue];
    if (values>1000)
    {
        unit = @"G";
        values = values/1000;
    }
    else if (values<1000)
    {
        unit = @"M";
    }
    if(![NSString checkNullString:dic[@"phone"]])
    {
        cell.ui_phone.text =[NSString stringWithFormat:@"%@ %@ %@",[dic[@"phone"] substringToIndex:3],
                             [dic[@"phone"] substringWithRange:NSMakeRange(3,4)],
                             [dic[@"phone"] substringFromIndex:7]];
    }
    else
    {
        cell.ui_phone.text = @"";
    }
    if ([dic[@"operateStatus"] isEqualToString:@"SUCCESS"])
    {
        cell.ui_static.text = @"成功";
        cell.ui_static.textColor = [ColorManager colorWithHexString:@"#E45252"];
    }
    else if ([dic[@"operateStatus"] isEqualToString:@"WAIT_FOR_CALL"])
    {
        cell.ui_static.text = @"等待回调";
        cell.ui_static.textColor = [ColorManager colorWithHexString:@"#E45252"];
    }
    else
    {
        cell.ui_static.text = @"失败";
        cell.ui_static.textColor = [ColorManager colorWithHexString:@"#000000" alpha:0.8];
    }
    if ([dic[@"operatorCode"]isEqualToString:@"10086"])
    {
        cell.ui_number.text = [NSString stringWithFormat:@"中国移动%ld%@ 编号:%@",values,unit,dic[@"spNo"]];
    }
    else if ([dic[@"operatorCode"]isEqualToString:@"10010"])
    {
        cell.ui_number.text = [NSString stringWithFormat:@"中国联通%ld%@ 编号:%@",values,unit,dic[@"spNo"]];
    }
    else if ([dic[@"operatorCode"]isEqualToString:@"10000"])
    {
        cell.ui_number.text = [NSString stringWithFormat:@"中国电信%ld%@ 编号:%@",values,unit,dic[@"spNo"]];
    }
    return cell;
}

- (XHBuyBackCell*)buyBackCell
{
    XHBuyBackCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHBuyBackCellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=0;
    if ([self.useInfoArray isKindOfClass:[NSArray class]])
    {
        if (self.useInfoArray.count!=0)
        {
            if (indexPath.section==0)
            {
                height = 167;
            }
            else if (indexPath.section==1)
            {
                height = 60;
            }
            else if(indexPath.section==2)
            {
                height = 90;
            }
        }
        else
        {
            if (indexPath.section==0)
            {
                 height = 167;
            }
            else if (indexPath.section==1)
            {
                height = 90;
            }
        }
    }
    else
    {
        if (indexPath.section==0)
        {
            height = 167;
        }
        else if (indexPath.section==1)
        {
            height = 90;
        }
    }

    return height;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0.1;
    return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
    CGFloat height = 10;
    return height;
}

#pragma - mark XHBuyBackCellDelegate

- (void)buyBackAction:(UIButton *)button
{
    XHBuyBackBounceViewController * viewController = [[XHBuyBackBounceViewController alloc]init];
    viewController.modalPresentationStyle = UIModalPresentationCustom;
    viewController.buyBackDic = self.backInfo;
    [self presentViewController:viewController animated:NO completion:^{
    }];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
